package com.akk.platform.oem.business;

import com.akk.module.avs.entity.AvsYbInfo;
import com.akk.module.avs.service.AvsYbInfoService;
import com.akk.utils.RestResponse;
import com.akk.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class YbInfoBusiness {

    @Autowired
    private AvsYbInfoService infoService;

    public RestResponse queryByAppId(){
        Map params = new HashMap();
        params.put("appId", ShiroUtils.getAdmin().getAppId());
        List<AvsYbInfo> list = infoService.queryByParams(params);
        AvsYbInfo info = null;
        if (list.size() == 1)
            info = list.get(0);
        return RestResponse.build(info);
    }

    public RestResponse update(AvsYbInfo info, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage());
        info.setAppId(ShiroUtils.getAdmin().getAppId());
        if (info.getInfoId() == null){
            infoService.save(info);
        }else {
            infoService.update(info);
        }
        return RestResponse.build("success");
    }

}
